//
// Created by pzw on 24-3-12.
//

#include "PriorityQueue.h"

template<typename E, typename Container, typename Compare>
JavaPriorityQueue<E, Container, Compare>::JavaPriorityQueue(
        std::priority_queue<E, Container, Compare> *que) : priority_queue(que) {
}

template<typename E, typename Container, typename Compare>
JavaPriorityQueue<E, Container, Compare>::~JavaPriorityQueue() {
    delete priority_queue;
}

template<typename E, typename Container, typename Compare>
bool JavaPriorityQueue<E, Container, Compare>::empty() {
    return (*priority_queue).empty();
}

template<typename E, typename Container, typename Compare>
E JavaPriorityQueue<E, Container, Compare>::peek() {
    return (*priority_queue).top();
}

template<typename E, typename Container, typename Compare>
E JavaPriorityQueue<E, Container, Compare>::poll() {
    E topElement = (*priority_queue).top();
    (*priority_queue).pop();
    return topElement;
}

template<typename E, typename Container, typename Compare>
void JavaPriorityQueue<E, Container, Compare>::offer(const E &item) {
    (*priority_queue).push(item);
}

template<typename E, typename Container, typename Compare>
int JavaPriorityQueue<E, Container, Compare>::search(const E &item) {
    /* Priority queue does not support search operation efficiently */
    return -1;
}

template
class JavaPriorityQueue<int>; // Explicit instantiation for int

int JavaPriorityQueue_main() {
    JavaPriorityQueue<int> *queue = new JavaPriorityQueue<int>(new std::priority_queue<int>());
    std::priority_queue<int> *pQueue = new std::priority_queue<int>();
    JavaPriorityQueue<int> queue2(pQueue);

    for (int i = 0; i < 8; ++i) {
        queue->offer(i);
        queue2.offer(i);
    }

    int poll = queue2.poll();
    std::cout << "JavaPriorityQueue_main: poll = " << poll << std::endl;

    int peek = queue2.peek();
    std::cout << "JavaPriorityQueue_main: peek = " << peek << std::endl;

    int search = queue2.search(8);
    std::cout << "JavaPriorityQueue_main: search position = " << search << std::endl;

    bool empty = queue2.empty();
    std::cout << "JavaPriorityQueue_main: empty = " << empty << std::endl;

    /* pQueue无需手动delete，因为我们的析构函数中，已经delete了 */
    delete queue;

    return 0;
}